/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: ListIndicators2EvaluateView.java
 * Date: 07/02/2011
 */

package com.ingenius.sau.client.view;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.ingenius.sau.client.Constants;
import com.ingenius.sau.client.event.ListFactors2EvaluateEvent;
import com.ingenius.sau.client.event.ListFeatures2EvaluateEvent;
import com.ingenius.sau.client.event.ListSelfEvaluationsEvent;
import com.ingenius.sau.client.presenter.ListIndicators2EvaluatePresenter.ListIndicators2EvaluateDisplay;
import com.ingenius.sau.client.view.widgets.LinkImage;
import com.ingenius.sau.client.view.widgets.LinkImage.LinkImageStatus;
import com.ingenius.sau.client.view.widgets.ListBox;
import com.ingenius.sau.shared.model.core.Indicator2Evaluate;
import com.ingenius.sau.shared.util.filter.Comparison;
import com.ingenius.sau.shared.util.filter.Filter;
import com.ingenius.sau.shared.util.filter.NumberFilter;
import com.ingenius.sau.shared.util.filter.StringFilter;

/**
 * Class <strong>ListIndicators2EvaluateView</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class ListIndicators2EvaluateView extends
		AbstractListView<Indicator2Evaluate> implements
		ListIndicators2EvaluateDisplay {

	// Singleton
	private static ListIndicators2EvaluateView instance;

	// UI Widgets
	private TextField<String> filterName = new TextField<String>();
	private TextField<String> filterDescription = new TextField<String>();
	private TextField<String> filterGradeFrom = new TextField<String>();
	private TextField<String> filterGradeTo = new TextField<String>();
	private ListBox filterWeighing = new ListBox();

	private LinkImage link1;
	private LinkImage link2;
	private LinkImage link3;
	private LinkImage link4;

	private Button detailsButton;

	private ListIndicators2EvaluateView() {
		super();
		link1 = new LinkImage("Evaluations", Constants.ICON_PROCESSES);
		link2 = new LinkImage("Factors", Constants.ICON_FACTORS_2_EVALUATE);
		link3 = new LinkImage("Features", Constants.ICON_FEATURES_2_EVALUATE);
		link4 = new LinkImage("Indicators",
				Constants.ICON_INDICATORS_2_EVALUATE);

		detailsButton = new Button("View Details");
		detailsButton.setIconStyle("icon-details");
		detailsButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				presenter.goInto();
			}
		});
	}

	public static ListIndicators2EvaluateView getInstance() {
		if (instance == null) {
			instance = new ListIndicators2EvaluateView();
		}
		return instance;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.view.AbstractListView#onRender(com.google.gwt
	 * .user.client.Element, int)
	 */
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		// TODO i18n
		gridContainer.setHeading("Indicators to evaluate");
		gridContainer.addButton(detailsButton);
		configureHeader();
		configureSubmenu();

		filterWeighing.setEditable(false);
		filterWeighing.add("---");
		for (int i = 1; i <= 10; i++) {
			filterWeighing.add("" + i);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.views.AbstractListView#createColumnModel()
	 */
	@Override
	protected ColumnModel createColumnModel() {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		// TODO i18n 2º columna
		ColumnConfig column = new ColumnConfig("name", "Name", 100);
		columns.add(column);

		column = new ColumnConfig("description", "Description", 300);
		columns.add(column);

		column = new ColumnConfig("weighing", "Weighing", 50);
		column.setColumnStyleName("columnCenter");
		column.setStyle("text-align: center;");
		columns.add(column);

		column = new ColumnConfig("grade", "Grade", 50);
		column.setColumnStyleName("columnCenter");
		column.setStyle("text-align: center;");
		columns.add(column);

		return new ColumnModel(columns);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.views.AbstractListView#createAdvancedSearch()
	 */
	@Override
	protected Widget createAdvancedSearch() {
		// TODO i18n
		final DisclosurePanel panel = new DisclosurePanel("Advanced search");
		panel.setStyleName("disclosure-panel");
		panel.setAnimationEnabled(false);

		FlexTable table = new FlexTable();
		table.setStyleName("advanced-search");
		table.setCellSpacing(12);
		table.setCellPadding(12);

		// TODO i18n
		table.setHTML(0, 0, "Name");
		table.setWidget(0, 1, filterName);

		table.setHTML(0, 2, "Description");
		table.setWidget(0, 3, filterDescription);

		table.setHTML(0, 4, "Weighing");
		table.setWidget(0, 5, filterWeighing);

		table.setHTML(1, 0, "Grade (From)");
		table.setWidget(1, 1, filterGradeFrom);

		table.setHTML(1, 2, "Grade (To)");
		table.setWidget(1, 3, filterGradeTo);

		HorizontalPanel buttons = new HorizontalPanel();
		buttons.setSpacing(10);
		buttons.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
		// TODO i18n
		Button searchButton = new Button("Search");
		searchButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				doSearch();
			}
		});
		// TODO i18n
		Button cancelButton = new Button("Cancel");
		cancelButton.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				doCancel();
				panel.setOpen(false);
			}
		});

		buttons.add(searchButton);
		buttons.add(cancelButton);

		VerticalPanel wrap = new VerticalPanel();
		wrap.add(table);
		wrap.add(buttons);
		wrap.setCellVerticalAlignment(buttons, HasAlignment.ALIGN_BOTTOM);
		wrap.setCellHorizontalAlignment(buttons, HasAlignment.ALIGN_CENTER);
		panel.add(wrap);

		return panel;
	}

	private void doSearch() {
		if (filters == null) {
			filters = new ArrayList<Filter>();
		}
		filters.clear();

		if (filterName.getValue() != null) {
			filters.add(new StringFilter(Comparison.EQUALS, "name", filterName
					.getValue()));
		}
		if (filterDescription.getValue() != null) {
			filters.add(new StringFilter(Comparison.EQUALS, "description",
					filterDescription.getValue()));
		}
		if (filterWeighing.getValue() != null) {
			try {
				Float value = Float.parseFloat(filterWeighing.getSimpleValue());
				filters.add(new NumberFilter(Comparison.EQUALS, "weighing",
						value));

			} catch (Exception e) {
				System.out.println("---Invalid numeric value.");
			}
		}
		if (filterGradeFrom.getValue() != null) {
			try {
				Float value = Float.parseFloat(filterGradeFrom.getValue());
				filters.add(new NumberFilter(Comparison.GREATER_THAN_OR_EQUAL,
						"grade", value));

			} catch (Exception e) {
				System.out.println("---Invalid numeric value.");
			}
		}
		if (filterGradeTo.getValue() != null) {
			try {
				Float value = Float.parseFloat(filterGradeTo.getValue());
				filters.add(new NumberFilter(Comparison.LESS_THAN_OR_EQUAL,
						"grade", value));

			} catch (Exception e) {
				System.out.println("---Invalid numeric value.");
			}
		}
		super.reload();
	}

	private void doCancel() {
		filterName.setValue("");
		filterDescription.setValue("");
		filterGradeFrom.setValue("");
		filterGradeTo.setValue("");
		filterWeighing.setSimpleValue("---");

		if (filters != null) {
			filters.clear();
			super.reload();
		}
	}

	protected void configureHeader() {
		// TDOD i18n
		String moduleName = "Indicators to Evaluate";
		String html = "<span class='module-header'>" + moduleName + "</span>";
		headerContainer.add(new Html(html));
	}

	protected void configureSubmenu() {
		link1.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				link1.setStatus(LinkImageStatus.enable);
				presenter.fireEvent(new ListSelfEvaluationsEvent());
			}
		});

		link2.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				if (LinkImageStatus.enable.equals(link2.getStatus())) {
					link2.setStatus(LinkImageStatus.enable);
					presenter.fireEvent(new ListFactors2EvaluateEvent());
				}
			}
		});

		link3.addListener(Events.OnClick, new Listener<BaseEvent>() {
			@Override
			public void handleEvent(BaseEvent be) {
				if (LinkImageStatus.enable.equals(link3.getStatus())) {
					link3.setStatus(LinkImageStatus.enable);
					presenter.fireEvent(new ListFeatures2EvaluateEvent());
				}
			}
		});

		link1.setStatus(LinkImageStatus.enable);
		link2.setStatus(LinkImageStatus.enable);
		link3.setStatus(LinkImageStatus.enable);
		link4.setStatus(LinkImageStatus.active);

		submenuContainer.add(link1);
		submenuContainer.add(link2);
		submenuContainer.add(link3);
		submenuContainer.add(link4);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.view.AbstractListView#enableButtons(java.lang
	 * .Boolean)
	 */
	@Override
	protected void enableButtons(Boolean state) {
		super.enableButtons(state);
		detailsButton.setEnabled(state);
	}
}
